from kafka import KafkaConsumer

if __name__ == '__main__':
    # 创建消费者
    kafka_consumer = KafkaConsumer(bootstrap_servers="master:9092,node2:9092,node2:9092",
                                   group_id="python",
                                   auto_offset_reset="earliest")
    # 订阅topic
    kafka_consumer.subscribe(topics=["car"])

    while True:
        # 拉取数据
        consumer_records = kafka_consumer.poll(timeout_ms=200)

        for topic, records in consumer_records.items():
            for record in records:
                # decode: 解码
                value = record.value.decode("UTF-8")
                offset = record.offset
                partition = record.partition
                print(partition, offset, value)
